package com.jams.goodox;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.Settings;
import android.support.multidex.MultiDexApplication;
import android.support.v4.app.NotificationManagerCompat;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.jams.goodox.activity.home.HomeActivity;
import com.jams.goodox.listen.SuccessListener;
import com.jams.goodox.utils.TipAlertDialog;
import com.jams.library.fresco.FreScoConfig;
import com.tencent.smtt.sdk.QbSdk;


/**
 * Created by jams on 2017/4/14.
 * ***
 * Tel: ***
 */
public class TownApplicationLike extends MultiDexApplication {

    private static Application mInstance = null;
    public static int width = 0;
    public static int height = 0;
    public static boolean IsDownLoadingApk;//是否正在下载apk
    protected ConnectionChangeReceiver myReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
//        MultiDex.install(TownApplicationLike.this);
        mInstance = TownApplicationLike.this;
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) TownApplicationLike.this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels; // 屏幕宽度（像素）
        height = dm.heightPixels; // 屏幕高度 （像素）
        Fresco.initialize(TownApplicationLike.this, FreScoConfig.getImagePipelineConfig(TownApplicationLike.this));// 图片缓存初始化配置


        setShareConfig();
        //QbSdk.initX5Environment(mInstance, null);
        registerReceiver();


    }

    //友盟分享
    private void setShareConfig() {
//        PlatformConfig.setWeixin(BuildConfig.WEIXIN_APPID, BuildConfig.WEIXIN_AppSecret);
//        //微信 appid appsecret
//        PlatformConfig.setQQZone(BuildConfig.QQ_APPID, BuildConfig.QQ_APPKEY);
        // QQ和Qzone appid appkey
    }

    public static Application getInstance() {
        return mInstance;
    }


    //网络监听
    class ConnectionChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                Toast.makeText(getApplicationContext(), "当前网络不可以用,请开启网络数据", Toast.LENGTH_SHORT).show();
            } else {
                //改变背景或者 处理网络的全局变量
                //ToastUtils.showCenterToast("网络正常",context);
            }
        }
    }

    protected void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

        myReceiver = new ConnectionChangeReceiver();
        this.registerReceiver(myReceiver, filter);
    }

    protected void unregisterReceiver() {
        if (myReceiver != null) {
            this.unregisterReceiver(myReceiver);
        }
    }

    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        super.onTerminate();
        unregisterReceiver();
    }
}
